package com.elvin.platform.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.elvin.platform.beans.Device;
import com.elvin.platform.beans.Dictionary;
import com.elvin.platform.beans.Organize;
import com.elvin.platform.service.DeviceService;
import com.elvin.platform.service.DictionaryService;
import com.elvin.platform.service.OrganizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RestController
@RequestMapping("/deviceManagerController")
@Log4j2
@Api("设备中心大屏管理")
public class DeviceManagerController {

    @Resource
    private DeviceService service;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private OrganizeService organizeService;

  @ApiOperation(value = "获取在线设备和离线设备数量")
    @PostMapping("/getOnlineNumber")
    public  Object  getOnlineNumber(
            @ApiParam(name="organizeId",value="组织id",required=true) @RequestParam("organizeId") Long organizeId,
            @ApiParam("设备类型") @RequestParam(value = "devType", required = false)String devType,
            @ApiParam("设备所属平台") @RequestParam(value = "devPlatform", required = false)String devPlatform,
            @ApiParam("设备场景") @RequestParam(value = "devScenarios", required = false)Integer devScenarios,
            @ApiParam("通讯方式") @RequestParam(value = "communicationMode", required = false)String communicationMode
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Device entity = new Device();
            entity.setOrganizeId(organizeId);
            entity.setDevType(devType);
            entity.setDevPlatform(devPlatform);
            entity.setDevScenarios(devScenarios);
            entity.setCommunicationMode(communicationMode);
            entity.setOnline(true);
            Integer onlineNumber = service.getOnlineNumber(entity);
            entity.setOnline(false);
            Integer offlineNumber = service.getOnlineNumber(entity);
            onlineNumber = onlineNumber==null?0:onlineNumber;
            offlineNumber = offlineNumber==null?0:offlineNumber;
            map.put("onlineNumber",onlineNumber==null?0:onlineNumber);
            map.put("offlineNumber",offlineNumber==null?0:offlineNumber);
            map.put("allNumber",(offlineNumber+onlineNumber));
            map.put("status", 0);
            map.put("message", "查询成功");

        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        return map;
    }


  @ApiOperation(value = "根据通讯方式统计")
    @PostMapping("/getDevTypeNumber")
    public  Object  getDevTypeNumber(
        @ApiParam(name="organizeId",value="组织id",required=true) @RequestParam("organizeId") Long organizeId,
        @ApiParam("设备类型") @RequestParam(value = "devType", required = false)String devType,
        @ApiParam("设备所属平台") @RequestParam(value = "devPlatform", required = false)String devPlatform,
        @ApiParam("设备场景") @RequestParam(value = "devScenarios", required = false)Integer devScenarios,
        @ApiParam("通讯方式") @RequestParam(value = "communicationMode", required = false)String communicationMode
    ) {
      Map<String, Object> map = new HashMap<String, Object>();
      try {
        Device entity = new Device();
        entity.setOrganizeId(organizeId);
        entity.setDevType(devType);
        entity.setDevPlatform(devPlatform);
        entity.setDevScenarios(devScenarios);
        entity.setCommunicationMode(communicationMode);
        List<Map<String, Object>> devTypeNumber = service.getDevTypeNumber(entity);
        map.put("data",devTypeNumber);
        map.put("status", 0);
        map.put("message", "查询成功");

      } catch (Exception e) {
        log.debug(e.getMessage());
      }
      return map;
    }


  @ApiOperation(value = "根据设备类型统计")
    @PostMapping("/getDevCommunicationModeNumber")
    public  Object  getDevCommunicationModeNumber(
        @ApiParam(name="organizeId",value="组织id",required=true) @RequestParam("organizeId") Long organizeId,
        @ApiParam("设备类型") @RequestParam(value = "devType", required = false)String devType,
        @ApiParam("设备所属平台") @RequestParam(value = "devPlatform", required = false)String devPlatform,
        @ApiParam("设备场景") @RequestParam(value = "devScenarios", required = false) Integer devScenarios,
        @ApiParam("通讯方式") @RequestParam(value = "communicationMode", required = false)String communicationMode
    ) {
      Map<String, Object> map = new HashMap<String, Object>();
      try {
        Device entity = new Device();
        entity.setOrganizeId(organizeId);
        entity.setDevType(devType);
        entity.setDevPlatform(devPlatform);
        entity.setDevScenarios(devScenarios);
        entity.setCommunicationMode(communicationMode);

        List<Map<String, Object>> devCommunicationModeNumber = service.getDevCommunicationModeNumber(entity);

        map.put("data",devCommunicationModeNumber);
        map.put("status", 0);
        map.put("message", "查询成功");

      } catch (Exception e) {
        log.debug(e.getMessage());
      }
      return map;
    }


  @ApiOperation(value = "根据场景统计")
  @PostMapping("/getDictionaryInfo")
  public  Object  getDictionaryInfo(
      @ApiParam(name="organizeId",value="组织id",required=true) @RequestParam("organizeId") Long organizeId,
      @ApiParam("设备类型") @RequestParam(value = "devType", required = false)String devType,
      @ApiParam("设备所属平台") @RequestParam(value = "devPlatform", required = false)String devPlatform,
      @ApiParam("设备场景") @RequestParam(value = "devScenarios", required = false)Integer devScenarios,
      @ApiParam("通讯方式") @RequestParam(value = "communicationMode", required = false)String communicationMode
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      Device entity = new Device();
      entity.setOrganizeId(organizeId);
      entity.setDevType(devType);
      entity.setDevPlatform(devPlatform);
      entity.setDevScenarios(devScenarios);
      entity.setCommunicationMode(communicationMode);
      //1.根据条件获取设备信息
      //2.获取对应信息的场景id
      //3.根据场景id循环查询字典包
      List<Device> deviceList = service.selectDeviceList(entity);
      List<Dictionary> list = new ArrayList<>();
      for (Device device : deviceList) {
        List<Dictionary> dictionaries = dictionaryService.selectDictionaryList(device.getDevScenarios());
        list.addAll(dictionaries);
      }

      ArrayList<Dictionary> distinctDictionary = list.stream().collect(Collectors.collectingAndThen(
          Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dictionary::getId))), ArrayList::new));
      for (Dictionary dictionary : distinctDictionary) {
        for (Dictionary dictionary5 : list) {
          if(dictionary.getId().equals(dictionary5.getId())){
            dictionary.setAcount(dictionary.getAcount()+1);
          }
        }
      }

      map.put("data",distinctDictionary);
      map.put("status", 0);
      map.put("message", "查询成功");

    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }



  @ApiOperation(value = "根据组织id统计场景")
  @PostMapping("/getDictionaryInfoByOrgId")
  public  Object  getDictionaryInfoByOrgId(
      @ApiParam(name="organizeId",value="组织id",required=true) @RequestParam("organizeId") Long organizeId
      ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      EntityWrapper<Organize> wrapper = new EntityWrapper<>();
      if (organizeId != null) {
        wrapper.eq("id", organizeId);
        List<Organize> organizeList = organizeService.selectList(wrapper);
        map.put("data",JSON.parseArray(organizeList.get(0).getDictionaryInfo(),Dictionary.class));
      }
      map.put("status", 0);
      map.put("message", "查询成功");

    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }

  public static void main(String[] args) {
    List<Dictionary> list = new ArrayList<>();
    Dictionary dictionary1 = new Dictionary();
    dictionary1.setId(1);
    dictionary1.setName("测试1");
    Dictionary dictionary2 = new Dictionary();
    dictionary2.setId(2);
    dictionary2.setName("测试2");


    Dictionary dictionary3 = new Dictionary();
    dictionary3.setId(3);
    dictionary3.setName("测试3");


    Dictionary dictionary4 = new Dictionary();
    dictionary4.setId(3);
    dictionary4.setName("测试3");

    list.add(dictionary1);
    list.add(dictionary2);
    list.add(dictionary3);
    list.add(dictionary4);
    Map map0 = new TreeMap();

    ArrayList<Dictionary> distinctDictionary = list.stream().collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dictionary::getId))), ArrayList::new));
    for (Dictionary dictionary : distinctDictionary) {
      for (Dictionary dictionary5 : list) {
        if(dictionary.getId() == dictionary5.getId()){
          dictionary.setAcount(dictionary.getAcount()+1);
        }
      }
    }
    for (Dictionary dictionary : distinctDictionary) {
      System.out.println(dictionary.getId()+""+dictionary.getName()+""+dictionary.getAcount());
    }
  }


  @ApiOperation(value = "获取场景获取在线设备和离线设备数量")
  @PostMapping("/getOnlineNumberBydevScenarios")
  public  Object  getOnlineNumberBydevScenarios(
      @ApiParam("设备场景") @RequestParam(value = "devScenarios", required = true)Integer devScenarios,
      @ApiParam("设备类型") @RequestParam(value = "devType", required = false)String devType,
      @ApiParam("设备所属平台") @RequestParam(value = "devPlatform", required = false)String devPlatform,
      @ApiParam("通讯方式") @RequestParam(value = "communicationMode", required = false)String communicationMode
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {

      Device entity = new Device();
      entity.setDevScenarios(devScenarios);
      entity.setDevType(devType);
      entity.setDevPlatform(devPlatform);
      entity.setCommunicationMode(communicationMode);
      entity.setOnline(true);
      int onlineNumber = service.selectDevicesBydevScenariosIdCount(entity);
      entity.setOnline(false);
      int offlineNumber = service.selectDevicesBydevScenariosIdCount(entity);
      map.put("onlineNumber",onlineNumber);
      map.put("offlineNumber",offlineNumber);
      map.put("allNumber",(offlineNumber+onlineNumber));
      map.put("status", 0);
      map.put("message", "查询成功");

    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }


}
